﻿
using RTW.DAL;
using RTW.OA.Models;
using RTW.Utils.Common;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class DeptServers
    {
	    private readonly IRepository<department> departmentRepository;
        private readonly IRepository<user> UserRepository;
        public DeptServers()
        {
            departmentRepository = new DapperRepository<department>();
            UserRepository = new DapperRepository<user>();
        }

        public List<department> GetData(string dEPT_NAME, string mANAGER, string g_DEPT)
        {
            var dstable = departmentRepository.GetAllEnumerable();
            if (!string.IsNullOrEmpty(dEPT_NAME))
            {
                dstable = dstable.Where(m => m.DEPT_NAME.Contains(dEPT_NAME));
            }
            if (!string.IsNullOrEmpty(mANAGER))
            {
                dstable = dstable.Where(m => m.MANAGER.Contains(mANAGER));
            }
            if (!string.IsNullOrEmpty(g_DEPT))
            {
                dstable = dstable.Where(m => m.G_DEPT == bool.Parse(g_DEPT));
            }
            return dstable.ToList();
        }

        internal List<SelectListItem> GetMANAGER()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            List<user> model = CacheHelper.Get<List<user>>(DTKeys.CACHE_USER);
            if (model == null)
            {
                CacheHelper.Insert(DTKeys.CACHE_USER, UserRepository.GetAllEnumerable().ToList());
                model = CacheHelper.Get<List<user>>(DTKeys.CACHE_USER);
            }
            tables.Clear();

            foreach (var dr in model)
            {
                string Id = dr.USER_ID.ToString();
                string Title = dr.TRUE_NAME.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }
            return tables;
        }

        public department GetModelById(string id)
        {
            return departmentRepository.GetById(id);
        }

		
        internal void Add(department model)
        {
            departmentRepository.Insert(model);
        }

        internal void Update(department model)
        {
            departmentRepository.Update(model);
        }

		internal void Del(string id)
        {
            departmentRepository.DeleteByIds(id);
        }

				      
        internal List<SelectListItem> GetDEPT_PARENT()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = departmentRepository.SqlWithParamsDynamic("select a.DEPT_ID Value,a.DEPT_NAME Text ,a.LEVEL from department a");
            tables.Clear();
            tables.Add(new SelectListItem() { Value = "0", Text = "最高部门" });

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                int ClassLayer = int.Parse(dr.LEVEL.ToString());
                string Title = dr.Text.ToString().Trim();

                if (ClassLayer == 0)
                {
                    tables.Add(new SelectListItem { Value = Id, Text = Title });
                }
                else
                {
                    Title = "├ " + Title;
                    Title = StringUtil.StringOfChar(ClassLayer, "　") + Title;
                    tables.Add(new SelectListItem { Value = Id, Text = Title });
                }
            }
            return tables;
        }


    }
}